<?php
namespace app\index\controller;
use think\Controller;
use think\Validate;
use think\Db;
use think\Session;
class Project extends Basic
{  

	
    public function index()
    {//




          $label= Db::name('vehicle_label')->where('types','1')->select();
	      $this->assign('label1',$label);
        
         $post=input("param.");
		 
		$date['status']="0";
		 foreach($label as $va){
	    $date['cheshang_id']=$va['id'];
        $project= Db::name('project')->where($date)->order('xmpx desc')->select();
		$project=sumorder($project);
		$arr[$va['type']]=$project;
		
	  }

         
     //  $project= Db::name('project')->where($date)->order('id desc')->paginate(10);;
	 $this->assign('project',$arr);
	
     return $this->fetch();
    }

	public function projectxy(){//投资协议
	$post=input("param.id");if(!$post){exit();}
	$id=Session::get('userid');
	 $project= Db::name('orderlist')->alias('a')->join('__PROJECT__ w','a.projectid=w.id')->join('__MEMBER__ d','a.userid=d.id')->order('id desc')->where('a.id',$post)->where(" a.userid='".$id."' ")->field('a.*,w.title,w.fenhong,w.days,d.name,d.bankcode,d.bankrealname')->find(); 
	 $this->assign('list',$project);
	  return $this->fetch();
	
	}
	public function projectid()
    {
      $post=input("param.id");
      if(!$post){exit();}
	 $project= Db::name('project')->where('id',$post)->select();
	 $project=sumorder($project);$project=$project[0];
	 $this->assign('project',$project);
	
	
       $userid=Session::get('userid');
	  $orderuser= Db::name('orderlist')->where('projectid="'.$post.'" and userid="'.$userid.'"')->find();
	  $this->assign('orderuser',$orderuser);
	
	 $this->assign('user',userarray());
	
     return $this->fetch();
    }
	public function projectidf(){
     $user=userarray();
     if(!$user){exit(json_encode(array('status'=>'0','msg'=>'请先登陆！','url'=>'/index/member/index')));}
	 $post=input('post.');
	 if($post){
		// return json($post);
	 $project= Db::name('project')->where('id',$post['id'])->find();
      $cheshang_id= Db::name('vehicle_label')->where('id',$project['cheshang_id'])->find();
       if($cheshang_id['userdj']>$user['userdj']){exit(json_encode(array('status'=>'0','msg'=>'会员等级不足！')));}
	
	 if(!$project){exit(json_encode(array('status'=>'0','msg'=>'非法操作！')));}
     if($project['status']!='0'){exit(json_encode(array('status'=>'0','msg'=>'该项目已完成')));}
	
	 $money=$post['money'];
	 if($money>$user['amount']){exit(json_encode(array('status'=>'0','msg'=>'余额不足，请充值！')));}
	 if($project['min_price']>$money){exit(json_encode(array('status'=>'0','msg'=>'该项目最少认筹金额 '.$project['min_price'].'！')));}
	 $max=$project['max_price'];
	 if($max<$money){exit(json_encode(array('status'=>'0','msg'=>'该项目最大认筹金额 '.$max.'！')));}
	 $ztzmoney=Db::name('orderlist')->where('projectid',$project['id'])->sum('money');//已投资金额
	  $symoney=$project['money']-$ztzmoney-$project['xuni_price'];//剩余可投资
	 if($symoney<0){$symoney=0;}
	 if($money>$symoney){exit(json_encode(array('status'=>'0','msg'=>'该项目剩余可投资金额 '.$symoney.'！')));}
	 if($project['sfft']=='1'){
	  $sfrc=Db::name('orderlist')->where(array('userid'=>$user['id'],'projectid'=>$project['id']))->find();
	  if($sfrc){exit(json_encode(array('status'=>'0','msg'=>'该项目不可复投 ！')));}
	  }

	      Db::startTrans();//事物开始
		  try{
             Db::name('member')->where('id',$user['id'])->setDec('amount',$money);
			 $config= Db::name('config')->find();
			 $zq=$project['days'];
			 if($project['fkfs']=='1'){$num='1';$day=$zq;}else{$num=$zq;$day="1";}
             $date['projectid']=$project['id'];
			 $date['userid']=$user['id'];
			 $date['money']=$money;
			 $date['addtime']=date('Y-m-d H:i:s');
		     $date['number']=$user['id']."_".time();
			 $date['fhnum']=$num;//总分红次数
			 if($config['tyfhsj']!=0){
				 if($config['tyfhsj']<10){
				 $config['tyfhsj']="0".$config['tyfhsj'];}
			     $date['fkdate']=date("Y-m-d",strtotime("+".$day." day"))." ".$config['tyfhsj'].":00:00";
				//return json($date);
				
			    
			 }else{
			   $date['fkdate']=date("Y-m-d H:i:s",strtotime("+".$day." day"));
			   if($config['fhjg']==0){ $date['fkdate']=date("Y-m-d H:i:s");}
			   

			 }
			 $insert=Db::name('orderlist')->insert($date);

			 
			 $ss['userid']=$user['id'];
             $ztzmoneys=Db::name('orderlist')->where($ss)->sum('money');//会员已投资金额
			 $dj=Db::name('memberdj')->select();//获取等级信息
			 $usedjid= Db::name('memberdj')->where('id',$user['id'])->find();
			 if(!$usedjid){$usedjid=0;}
			 $djid=0;$userdjid='';
			 foreach($dj as  $djva){
			   if($djva['tzmoney']>$djid){
			     
                    if($ztzmoneys>=$djva['tzmoney'] and $usedjid['tzmoney']<$djva['tzmoney']){
				     $djid=$djva['tzmoney'];
					 $userdjid=$djva['id'];
				    }
			   } 
			 }
			 if($userdjid!=''){
			 
			  Db::name('member')->where('id',$user['id'])->update(array('userdj'=>$userdjid));//修改等级
			 }

			 $orderlistid = Db::name('orderlist')->getLastInsID();//获取返回主键值
			 $projectdate['num']=$project['num']+1;//该项目加人数
			
			
			 if(($ztzmoney+$money)>=$project['money']){//众筹金额已满修改状态为出售中记录出售起始时间
			 $projectdate['status']='1';
			 
			 }
			   Db::name('project')->where('id',$project['id'])->update($projectdate);
               addmoneylog($user['id'],"加入：".$project['title']."项目 扣除金额",$money,'-');//添加金额记录
			 
			 projectfd($money,Session::get('userid'),$config['zcfdbl'],array('id'=>$user['id'],'tzmoney'=>$money));//结算反点
			 projectfh();//所有订单分红
			//return json($aa);die;   
           
		    Db::commit();//提交事件
          
      
	       }catch (\Exception $e) {
			  
			  
			  // 回滚事务
			  
			   Db::rollback();
			   
			 exit(json_encode(array('status'=>'0','msg'=>'未知错误,数据已回滚！')));
			}

	 
	 }
	 exit(json_encode(array('status'=>'1','msg'=>'投资成功')));
	
   }


   
	
}
